package com.snail.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.snail.model.pojo.BizTask;
import com.snail.model.vo.TaskVo;
import com.snail.response.BaseResponse;
import com.snail.response.PageResponse;
import com.snail.response.ResultUtils;
import com.snail.service.BizTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author: cwl
 * @Description: BizTask增删查改接口
 * @DateTime : 2025/7/23 17:31
 **/
@RestController
@RequestMapping("/task")
public class BizTaskController {

    @Autowired
    private BizTaskService bizTaskService;

    // 多条件分页查询
    @GetMapping("/list")
    public PageResponse<TaskVo> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer taskType,
            @RequestParam(required = false) Integer difficultyLevel,
            @RequestParam(required = false) Integer needReview,
            @RequestParam(required = false) String keyword
    ) {
        Page<TaskVo> resultPage = bizTaskService.queryTasks(page, size, status, taskType, difficultyLevel, needReview, keyword);
        return ResultUtils.pageSuccess(resultPage);
    }

    // 新增任务
    @PostMapping("/add")
    public BaseResponse<Boolean> add(@RequestBody BizTask task) {
        task.setCreateTime(new Date());
        task.setUpdateTime(new Date());
        boolean result = bizTaskService.save(task);
        return ResultUtils.success(result);
    }

    // 修改任务
    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody BizTask task) {
        task.setUpdateTime(new Date());
        boolean result = bizTaskService.updateById(task);
        return ResultUtils.success(result);
    }

    // 删除任务（逻辑删除）
    @PostMapping("/delete/{id}")
    public BaseResponse<Boolean> delete(@PathVariable Long id) {
        boolean result = bizTaskService.logicDelete(id);
        return ResultUtils.success(result);
    }

    // 任务详情
    @GetMapping("/{id}")
    public BaseResponse<BizTask> detail(@PathVariable Long id) {
        BizTask task = bizTaskService.getById(id);
        return ResultUtils.success(task);
    }
}
